package stat;

import java.util.ArrayList;
import java.util.Map;

public class Goodput {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		String file = "scripts/gnuplot/logs/stat_rarest_first.log";
//		String file = "scripts/gnuplot/logs/stat_sliding_window.log";
		String file = "scripts/gnuplot/logs/stat_segments.log";

		//read log
		Map map = LogReader.getMap(file);
		
		//number of peers
		int nrPeers = map.size();
		
		//size of time array
		long firstTimeValue = 0;
		int size = 0;
		boolean firstTime = false;
		for(Object peer : map.keySet()) { //each peer
			ArrayList<LogReaderLine> lines = (ArrayList<LogReaderLine>) map.get((String)peer);
			for(LogReaderLine lrl : lines) {
				size++;
				
				if(!firstTime) {
					firstTimeValue = lrl.getTimestamp();
				}
				
				firstTime = true;
			}
			break;
		}
		
		//time array
		long[] time = new long[size];
		
		//calculate X;Y sets block arrival times
		for(Object peer : map.keySet()) { //each peer
			ArrayList<LogReaderLine> lines = (ArrayList<LogReaderLine>) map.get((String)peer);
			int i = 0;
			for(LogReaderLine lrl : lines) {
				time[i] += lrl.getTimestamp();
				i++;
			}
		}
		
		int j = 0;
		for(long val : time) {
			System.out.println(((val/nrPeers)-firstTimeValue)/1000+";"+j);
			j++;
		}
		
		/*
		 * Calculate goodput constant
		 */
		System.out.println("");
		int videoSizeKb = 192600;
		int videoLength = (int) (18.57*60); 
		int numberOfChunks = 770;
		
		float oneSecond = videoSizeKb / videoLength;
		float pieceInSeconds = oneSecond/(videoSizeKb/numberOfChunks);
		System.out.println("Piece length in seconds (goodput constant): "+pieceInSeconds);
	}
}
